#include "RingQueue.hpp"
#include <functional>
#include <iostream>
#include <unistd.h>




using namespace std;

using task_t= function<void()>;
RingQueue<task_t> bq(2);

void task1()
{
    printf("处理数据库任务\n");
}

void task2()
{
    printf("处理网络任务\n");
}

void* routine1(void* args)
{
    char* p= (char*)args;
    while(1)
    {
        sleep(1);
        printf("%s 往阻塞队列添加任务\n",p);
        bq.Enqueue(task1);
    }
}

void* routine3(void* args)
{
    char* p= (char*)args;
    while(1)
    {
        sleep(1);
        printf("%s 往阻塞队列添加任务\n",p);
        bq.Enqueue(task2);
    }
}

void* routine2(void* args)
{
    char* p= (char*)args;
    while(1)
    {
        sleep(1);
        printf("%s ",p);
        task_t t;
        bq.Pop(&t);
        t();
    }
}
int main()
{

    pthread_t t1,t2,t3,t4;
    pthread_create(&t1, nullptr, routine1, (void*)"thread 1");
    pthread_create(&t2, nullptr, routine3, (void*)"thread 2");
    pthread_create(&t3, nullptr, routine2, (void*)"thread 3");
    pthread_create(&t4, nullptr, routine2, (void*)"thread 4");
    

    while(1)
    {
        sleep(5);
    }


    return 0;
}